#include "Barrel.h"
#include <iostream>

Barrel::Barrel() {
}
Barrel::~Barrel() {
}

void Barrel::Initialize(sf::RenderWindow* window, int Pos, sf::Texture* texture, sf::Texture* splashTexture, sf::Texture* bloodTexture) {
	m_renderWindow = window;
	m_isDead = false;
	m_hit = false;
	m_clock = new sf::Clock();
	m_pos = Pos;
	m_speed = 0.9f;
	m_scale = 1.006;
	m_brlSprite = new sf::Sprite();
	m_brlSprite->setTexture(*texture);
	m_alphaBlood = 200.f;

	m_bloodSprite = new sf::Sprite;
	m_bloodSprite->setTexture(*bloodTexture);

	m_hitEffect = 0.f;
	m_hitSprite = new sf::Sprite();
	m_hitSprite->setTexture(*splashTexture);

	m_anim = new Animation();
	m_anim->Initialize(splashTexture, sf::Vector2f(500.f, 500.f), 6, 20, 0);

	if(m_pos == 1){
		m_brlSprite->setPosition(900,700);
	}else if(m_pos == 2){
		m_brlSprite->setPosition(930,700);
	}else if(m_pos == 3){
		m_brlSprite->setPosition(950,700);
	}else if(m_pos == 4){
		m_brlSprite->setPosition(980,700);
	}

	m_brlSprite->setOrigin(texture->getSize().x/2, texture->getSize().y);
	m_brlSprite->setScale(0.06, 0.06); 
}

void Barrel::Update(Player* player) {
	m_deltaTime = m_clock->restart().asSeconds();
	//std::cout << 1/m_deltaTime << std::endl;
	
	if(m_brlSprite->getGlobalBounds().top < 1070){
		if(m_brlSprite->getGlobalBounds().top < 1020){
			if(m_brlSprite->getGlobalBounds().intersects(player->getCollisionRect()->getGlobalBounds()) && m_hit == false){
			//	std::cout << "bara ett test" << std::endl; 
				m_hit = true;
				player->takeDamage(10);
				m_hitSprite->setScale(1.5f, 1.5f);	
			}
		}

		m_renderWindow->draw(*m_brlSprite);

		if(m_hit == true && m_hitEffect < 0.2f){
			m_hitSprite->setPosition(player->getSprite()->getGlobalBounds().left + 20.f, player->getSprite()->getGlobalBounds().top - 40.f);
			m_anim->Update(m_deltaTime);
			m_hitSprite = m_anim->GetSprite();
			m_renderWindow->draw(*m_hitSprite);
			m_hitEffect = m_hitEffect + m_deltaTime;	
		}

		if(m_brlSprite->getGlobalBounds().top + m_brlSprite->getGlobalBounds().height < 1040){
			if(m_brlSprite->getScale().x < 0.25f){
				m_brlSprite->scale(m_scale, m_scale);
			}
		}else{
			if(m_speed < 1.7f){
				m_speed += 10.f * m_deltaTime;
			}
			if(m_brlSprite->getScale().x < 0.25f){
				m_brlSprite->scale(m_scale, m_scale);
			}
		}

		if(m_pos == 1){
			m_brlSprite->move(-0.84f, m_speed);
		}else if(m_pos == 2){
			m_brlSprite->move(-0.31f, m_speed);
		}else if(m_pos == 3){
			m_brlSprite->move(0.31f, m_speed);
		}else if(m_pos == 4){
			m_brlSprite->move(0.84f, m_speed);
		}
	  if(m_hit == true){
		  m_alphaBlood -= 80*m_deltaTime;
		  m_bloodSprite->setPosition(player->getSprite()->getPosition().x - 1000.f, 0.f);
		  m_bloodSprite->setColor(sf::Color(m_bloodSprite->getColor().r, m_bloodSprite->getColor().g, m_bloodSprite->getColor().b, m_alphaBlood));
		  m_renderWindow->draw(*m_bloodSprite);
	  }

	}else{
		m_isDead = true;
	}
}

void Barrel::Render() {

}

void Barrel::Reset() {
	if(m_pos == 1){
		m_brlSprite->setPosition(900,700);
	}else if(m_pos == 2){
		m_brlSprite->setPosition(930,700);
	}else if(m_pos == 3){
		m_brlSprite->setPosition(950,700);
	}else if(m_pos == 4){
		m_brlSprite->setPosition(980,700);
	}
	m_isDead = false;
	m_hit = false;
	m_speed = 0.9f;
	m_scale = 1.006;
	m_hitEffect = 0.f;
	m_brlSprite->setScale(0.06, 0.06); 
		
}

void Barrel::Cleanup() {
	delete m_brlSprite;
	delete m_hitSprite;
	delete m_clock;
	delete m_bloodSprite;
}